package com.bbs.service.impl;

import com.bbs.common.QueryResult;
import com.bbs.common.ResponseResult;
import com.bbs.domain.User;
import com.bbs.entity.Result;
import com.bbs.mapper.UserMapper;
import com.bbs.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private  UserMapper userMapper;

    /**
     * 后台管理登录
     * @param loginUser
     * @return
     */
    @Override
    public ResponseResult manageLogin(User loginUser) {

        if (!"admin".equals (loginUser.getUsername())){
            return new ResponseResult(false,0,"登陆失败!用户名为非管理员用户");
        }

        User user = userMapper.selectOne(loginUser);

        if (user == null ){
            return new ResponseResult(false,-1,"登录失败!用户名或密码不正确");
        }


        return new ResponseResult(true,1,"登录成功!");
    }

    /**
     * 查询所有用户列表
     * @return
     */
    @Override
    public List<User> findAll() {

        return userMapper.selectAll();
    }

    @Override
    public QueryResult<User> findPage(int page, int size) {

        PageHelper.startPage(page,size);

        Page<User> users = (Page<User>) userMapper.selectAll();

        QueryResult<User>  results = new QueryResult<>();

        results.setList(users);
        results.setTotal(users.getTotal());

        return results;
    }



    /**
    * @author chen hong wei
    * @Description
    * @Date 11:24 2019/9/22
    * @Param [username]用户名 和状态为以
    * @return void
    **/
    @Override
    public User findUser( String username) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",username);
        List<User> users = userMapper.selectByExample(example);
        if(users==null&&users.size()<=0){
            throw new RuntimeException("运行错误");
        }
        return users.get(0);

    }
    /**
     * @author chen hong wei
     * @Description 更改用户权限
     * @Date 14:14 2019/9/22
     * @Param [user]
     * @return void
     **/
    @Override
    public void update(User user) {
        user.setRole(2);//申请成为高级用户
        userMapper.updateByPrimaryKeySelective(user);
    }


    @Override
    public Map<String, Object> findUserByLoginStatus() {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("loginstatus","1");
        List<User> users = userMapper.selectByExample(example);
        Map map = new HashMap();
        map.put("loginUser",users);
        return map;
    }

    /***
     * 用户注册
     */
    @Override
    public Result userRegister(User user) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",user.getUsername());

        List<User> userList = userMapper.selectByExample(example);
        User user1 = userList.get(0);
        //对用户名和密码非空进行判断,同时user1要为空,才可以注册
        if (user1.getUsername() != null && user1.getUserpass()!=null && user1== null) {

            //将user保存到数据库
            userMapper.insertSelective(user);

            return new Result(0,"注册成功");
        }else{
            return new Result(1,"注册失败");
        }

    }

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    @Override
    public User findUserByUsername(String username) {
        User user = new User();
        user.setUsername(username);
        List<User> userList = userMapper.select(user);
        if (userList.size()>0){
            return userList.get(0);
        }else {
            return new User();
        }
    }
}
